package org.lc.oauth.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.OAuthFlow;
import io.swagger.v3.oas.models.security.OAuthFlows;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

@Configuration
public class SwaggerConfig {

    @Value("${knife4j.gateway.tokenUrl:''}")
    private String tokenUrl = "";

    @Bean
    public OpenAPI springOpenAPI() {
        Contact contact = new Contact();
        contact.setEmail("aiyunxz@163.com");
        contact.setName("代亮");
        contact.setUrl("www.lmixc.com");

        Info info = new Info();
        info.title("授权模块")
                .contact(contact)
                .description("主要用于客户端注册等操作")
                .version("v1.0.0");

        return new OpenAPI()
                .info(info).addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
                .components(new Components().addSecuritySchemes(HttpHeaders.AUTHORIZATION, new SecurityScheme()
                        .name(HttpHeaders.AUTHORIZATION).type(SecurityScheme.Type.OAUTH2).scheme("bearer")
                        .in(SecurityScheme.In.HEADER)
                        .flows(new OAuthFlows()
                                .password(new OAuthFlow().tokenUrl(tokenUrl)))
                ));
    }
}
